package com.example.app;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtil {
    public static class Holder {
        public static final SPUtil INSTANCE = new SPUtil();
    }
    private SPUtil() {}
    public static SPUtil getInstance() {
        return Holder.INSTANCE;
    }

    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;

    public void initialize(Context context) {
        mContext = context;
    }

    public void putMqtthost(String value) {
        SharedPreferences preferences = mContext.getApplicationContext().getSharedPreferences("XXXX", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("mqtt_host", value);
        editor.commit();
        editor.apply();
    }

    public String getMqtthost() {
        SharedPreferences preferences = mContext.getApplicationContext().getSharedPreferences("XXXX", Context.MODE_PRIVATE);
        String value = preferences.getString("mqtt_host", null);
        return value;
    }

    public void cleanMqtthost()
    {
        SharedPreferences preferences = mContext.getApplicationContext().getSharedPreferences("XXXX", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear(); // 清除所有键值对
        editor.apply();
    }
}
